<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\TestingItem;
use TestingDetailService;
use TestingConfig;

class TestingDetailItemsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_item.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.testing_item.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_item.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TestingItem::class, function (Grid $grid) {
            $grid->model()->orderBy('index', 'asc');
            $this->buildGrid($grid);
            $grid->disableCreation();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TestingItem::class, function (Form $form) {
            $form->text('index',trans('backend.testing_item.column.index'));
            $form->text('title',trans('backend.testing_item.column.title'));
            //此选项是从数据库中进行读取的，对应的key为question_type
            $questionTypeOptions = TestingConfig::buildSelectOptions('question_type');
            $form->select('question_type',trans('backend.testing_item.column.question_type'))->options($questionTypeOptions);
            $form->select('testing_id',trans('backend.testing_item.column.testing_id'))->options('/admin/testing/detail/options');
            //此选项是从数据库中进行读取的，对应的key为question_type
            $middleVarOptions = TestingConfig::buildSelectOptions('middle_var');
            $form->select('middle_var',trans('backend.testing_item.column.middle_var'))->options($middleVarOptions);
            $form->text('options',trans('backend.testing_item.column.options'));
            $form->text('expression',trans('backend.testing_item.column.expression'));
        });
    }

    public function buildGrid($grid){
        $grid->id('ID')->sortable();
        $grid->index(trans('backend.testing_item.column.index'));
        $grid->column('title',trans('backend.testing_item.column.title'));
        $grid->question_type(trans('backend.testing_item.column.question_type'))->value(function($questionType){
            return TestingConfig::convertOptions('question_type',$questionType);
        });
//        $grid->testing_id(trans('backend.testing_item.column.testing_id'))->vaule(function($testing_id){
//            $detail = TestingDetailService::getById($testing_id);
//            return $detail['name'];
//        });

        $grid->detail()->name(trans('backend.testing_item.column.testing_id'));

        $grid->middle_var(trans('backend.testing_item.column.middle_var'))->value(function($midVar){
            return TestingConfig::convertOptions('middle_var',$midVar);
        });

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->useModal();
            $filter->like('detail.name',trans('backend.testing_item.column.testing_id'));
        });

//        $grid->options(trans('backend.testing_item.column.options'));
//        $grid->expression(trans('backend.testing_item.column.expression'));
    }

    protected function singleTestingGrid($tid){
        return Admin::grid(TestingItem::class, function (Grid $grid) use ($tid) {
            $grid->model()->where('testing_id','=',$tid);
            $grid->model()->orderBy('index', 'asc');
            $this->buildGrid($grid);
        });
    }

    public function singleTesting($id){
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.testing_item.title.list'));
            $content->body($this->singleTestingGrid($id));
        });
    }


}
